﻿
@{
    ViewBag.Title = "跨域测试";
}

<h2>跨域测试</h2>
<div class="row" style="margin-top: 50px">
    <div class="col-lg-6">
        <div class="input-group">
            <input type="text" class="form-control" placeholder="先点击按钮" id="txtVal" />
            <span class="input-group-btn">
                <button class="btn btn-secondary" type="button" id="btnGet">
                    获取
                </button>
            </span>
        </div>
    </div>
</div>

@section scripts{
    <script src="~/Scripts/jquery.jsonp.js"></script>
    <script>
        $(function () {
            var $doc = {
                $btn: $('#btnGet'),
                $txt: $('#txtVal'),
                init: function () {
                    $doc.$btn.click(function () {
                        //$doc.$txt.val(111);
                        //$doc.jsonpGet();
                        $.getJSON('/Proxy',
                            { reqUrl: 'http://localhost:26770/home/name' },
                            function (data) {
                                var obj = eval(data);
                                $doc.$txt.val(obj.Name);
                        });
                    });
                },
                jsonpGet: function () {
                    $.jsonp({
                        url: 'http://localhost:26770/home/name',
                        callbackParameter: 'callback',
                        success: function (data) {
                            var obj = eval(data);
                            $doc.$txt.val(obj.Name);
                        },
                        error: function (xOptions, textStatus) {

                        }

                    });
                }
            };

            $doc.init();
        });
    </script>
    
    }

